import { Component, OnInit } from '@angular/core';
import { Router,ActivatedRoute } from '@angular/router';
import {HttpRouterService} from "../service/http-router.service";
import {HttpService} from "../service/http.service";
import { StorageService } from '../service/storage.service';
import * as _ from 'underscore';


@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less']
})
export class LoginComponent implements OnInit {
  public choose_amount = 0;
  public mobile = "";
  public captcha = "";
  public isAccredit=true;
  public gender = '1';
  public birthday = "";
  public realname = "";
  public source = "";
  public isPublic = false;
  public loginip = "61.148.244.227";
  constructor(
    private _http:HttpService,
    private _router: Router,
    private _activatedRoute: ActivatedRoute,
    private _storage: StorageService,
    private _httpRouter:HttpRouterService
  ) { }

  ngOnInit() {
    this._activatedRoute.queryParams.subscribe(params => {
      this.source = params['source'] || "";
    });

  }


  sendSms($event){
    $event.stopPropagation();
    if(!this.mobile){
      alert("请输入手机号！");
      return;
    }
    let param = "mobile="+this.mobile;
    this._http.post(this._httpRouter.sendSms,param).then(res => {
      if(res.code == 2){
        this.isPublic = true;
      }
      if(res.data){
        this.loginip = res.data ? res.data.userip : this.loginip;
        this._storage.setSession("loginip", this.loginip);
        this._storage.setSessionObject("customer", res.data);
      }


      alert(res.msg);
    })
  }


  setPhone($event){
    $event.stopPropagation();

    var patt = /[0-9]{4}\-[0-9]{2}\-[0-9]{2}/;
    if(!this.realname){
      alert("请输入姓名！");
      return;
    }

    var reg=/^[\u2E80-\u9FFF]+$/;//Unicode编码中的汉字范围
    if(!reg.test(this.realname)){
      alert("请输入中文！");
      return;
    }


    if(!this.birthday){
      alert("请输入生日！");
      return;
    }


    if (!patt.test(this.birthday)){
      alert("生日格式为1900-01-01");
      return;
    }

    if(this.choose_amount == 0){
      alert("请选择贷款金额！");
      return;
    }

    if(!this.mobile){
      alert("请输入手机号！");
      return;
    }

    if(!this.captcha){
      alert("请输入验证码！");
      return;
    }
    if(!this.isAccredit){
      alert("请勾选申请有礼，送最高百万意外保险，平安出行。");
      return;
    }

    this.datasuv();

    let param = "mobile="+this.mobile+"&captcha="+this.captcha+"&money="+this.choose_amount
      +"&realname="+this.realname
      +"&gender="+this.gender
      +"&birthday="+this.birthday
      +"&source="+this.source;
    this._http.post(this._httpRouter.setPhone,param).then(res => {
      this._storage.setSession("customerId",res.data.id);
      this._storage.setSessionObject("customer",res.data);
      this._storage.setSession("mobile", this.mobile);
      if(res.code==1){

        if(this.choose_amount < 10000){
          //this._router.navigate(['/download']);
          this._router.navigate(['/member/product']);
        }else{
          this._router.navigate(['/regist']);
        }
      }else{
        this._router.navigate(['/member/product']);
      }
      this.isPublic = false;

    })
  }
  accredit(){
    this.isAccredit = this.isAccredit ? false : true;
    this._storage.setSession("isAccredit", this.isAccredit);
  }

  datasuv(){

    let extentedParam = [{
      'client_ip':this.loginip
    }]

    let param = "name="+this.realname
      +"&gender="+this.gender
      +"&mobile="+this.mobile
      +"&birthday="+this.birthday
      +"&extentedParam="+JSON.stringify(extentedParam);


    this._http.post(this._httpRouter.datasuv,param).then(res => {
      let param1 = "event='ZX'"
        +"&param="+this.realname+","+this.gender+","+this.mobile+","+this.birthday+","+this.loginip
        +"&return="+res.msg
      this._http.post(this._httpRouter.log,param1).then(res => {
      })
    })
  }



}
